package com.sunday.volunteer.ui.user;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.sunday.common.event.EventBus;
import com.sunday.common.imageselector.MultiImageSelectorActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.Constants;
import com.sunday.common.utils.ImageUtils;
import com.sunday.common.utils.StringUtils;
import com.sunday.common.utils.TimeCount;
import com.sunday.common.utils.ToastUtils;
import com.sunday.member.base.BaseActivity;
import com.sunday.member.utils.SharePerferenceUtils;
import com.sunday.member.utils.UploadUtils;
import com.sunday.volunteer.BaseApp;
import com.sunday.volunteer.R;
import com.sunday.volunteer.entity.Images;
import com.sunday.volunteer.entity.Member;
import com.sunday.volunteer.event.RegistSuccess;
import com.sunday.volunteer.http.HttpClient;
import com.sunday.volunteer.ui.MainActivity;
import com.sunday.volunteer.utils.BaseUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by admin on 2017/1/10.
 */

public class RegisterActivity extends BaseActivity{

    @Bind(R.id.name)
    EditText name;
    @Bind(R.id.id_card)
    EditText id_card;
    @Bind(R.id.mobile)
    EditText mobile;
    @Bind(R.id.code)
    EditText code;
    @Bind(R.id.img1)
    ImageView img1;
    @Bind(R.id.img2)
    ImageView img2;
    @Bind(R.id.get_code)
    TextView get_code;
    @Bind(R.id.password)
    EditText password;
    @Bind(R.id.password2)
    EditText password2;
    private String mobileNo;
    private String imgpath1,imgpath2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);

    }

    @OnClick(R.id.get_code)
    void getCode(){
        if(!StringUtils.isMobileNO(mobile.getText().toString())){
            ToastUtils.showToast(mContext , "手机号不合法");
            return;
        }
        mobileNo = mobile.getText().toString();
        Call<ResultDO> call = HttpClient.getHttpAdapter().sendCode(mobileNo);
        call.enqueue(new Callback<ResultDO>() {
            @Override
            public void onResponse(Call<ResultDO> call, Response<ResultDO> response) {
                if(response.body() == null ||isFinish){
                    return;
                }
                if(response.body().getCode() == 0){
                    ToastUtils.showToast(mContext, "验证码已发送");
                    new TimeCount(60000, 1000, get_code ).start();
                }else {
                    ToastUtils.showToast(mContext , response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO> call, Throwable t) {

            }
        });

    }


    public void Register(View view){
        if(TextUtils.isEmpty(imgpath1)||TextUtils.isEmpty(imgpath2)){
            ToastUtils.showToast(mContext , "请选择身份证正反面");
            return;
        }
        if(TextUtils.isEmpty(name.getText().toString())){
            ToastUtils.showToast(mContext , "请输入姓名");
            return;
        }
        if(TextUtils.isEmpty(id_card.getText().toString())){
            ToastUtils.showToast(mContext , "请输入身份证号码");
            return;
        }
        if(!StringUtils.isMobileNO(mobileNo)){
            ToastUtils.showToast(mContext , "手机号不合法");
            return;
        }
        if(TextUtils.isEmpty(code.getText().toString())){
            ToastUtils.showToast(mContext , "请输入验证码");
            return;
        }
        upload();

    }

    private int imgs = 1;

    public void Img1(View view){
        imgs = 1;
        toImage();
    }


    public void Img2(View view){
        imgs = 2;
        toImage();
    }

    public void toImage(){
        intent = new Intent(mContext, MultiImageSelectorActivity.class);
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, 1);
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_SINGLE);
        startActivityForResult(intent, 1111);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 1111:
                    final ArrayList<String> mSelectPath = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
                    String imgPath = ImageUtils.getCompressImagePath(mSelectPath.get(0));
                    if(imgs == 1){
                        imgpath1 = imgPath;
                        img1.setImageBitmap(BitmapFactory.decodeFile(imgpath1));
                    }else {
                        imgpath2 = imgPath;
                        img2.setImageBitmap(BitmapFactory.decodeFile(imgpath2));
                    }


                    return;
            }
        }
    }


    //上传图片
    private void upload(){
        showLoadingDialog(R.string.upload_img);

        List<String> imgPath = new ArrayList<>();
        imgPath.add(imgpath1);
        imgPath.add(imgpath2);
        Call<ResultDO<List<Images>>> call =  HttpClient.getHttpAdapter().uploadMultiFile(UploadUtils.getRequestBody(imgPath));
        call.enqueue(new Callback<ResultDO<List<Images>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<Images>>> call, Response<ResultDO<List<Images>>> response) {
                dissMissDialog();
                if (response.body() == null||isFinish) {
                    return;
                }
                ResultDO<List<Images>> resultDO = response.body();
                if (resultDO.getCode() == 0) {
                        imgpath1 = resultDO.getResult().get(0).getHalfPath();
                        imgpath2 = resultDO.getResult().get(1).getHalfPath();
                        Log.d("iiii" , imgpath1 +"\n"+imgpath2 );
                    register();
                }
            }
            @Override
            public void onFailure(Call<ResultDO<List<Images>>> call, Throwable t) {

            }
        });
    }

    private void register(){
        Call<ResultDO<Member>> call = HttpClient.getHttpAdapter().register(mobileNo , password.getText().toString() ,name.getText().toString() ,
                id_card.getText().toString() , imgpath1, imgpath2,code.getText().toString());
        call.enqueue(new Callback<ResultDO<Member>>() {
            @Override
            public void onResponse(Call<ResultDO<Member>> call, Response<ResultDO<Member>> response) {
                if (response.body() == null||isFinish) {
                    return;
                }
                ResultDO<Member> resultDO = response.body();
                if (resultDO.getCode() == 0) {
                    SharePerferenceUtils.getIns(mContext).saveOAuth(resultDO.getResult());
                    BaseApp.getInstance().setMemberId(resultDO.getResult().getId());
                    SharePerferenceUtils.getIns(mContext).putBoolean(Constants.IS_LOGIN, true);

                    EventBus.getDefault().post(new RegistSuccess());
                    BaseUtil.openActivity(mContext , MainActivity.class);
                    finish();
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Member>> call, Throwable t) {

            }
        });
    }




}
